package com.tsd.system.service.impl;

import com.tsd.CommonMsgNameConstants;
import com.tsd.CommonParamNameConstants;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysDataObjAccessRuleMapper;
import com.tsd.system.dao.SysDataObjMapper;
import com.tsd.system.entity.SysDataObj;
import com.tsd.system.entity.SysDataObjAccessRuleExt;
import com.tsd.system.entity.SysDataObjExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysDataObjService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysDataObj 服务实现层
 *
 * @author david
 * @date 2021/07/08 15:49:18
 */
@Service
public class SysDataObjServiceImpl extends BaseServiceImpl implements SysDataObjService {

    @Resource
    private SysDataObjMapper sysDataObjMapper;
    @Resource
    private SysDataObjAccessRuleMapper sysDataObjAccessRuleMapper;

    @Override
    public SysDataObj getSysDataObjBySid(String sid) throws Exception {
        return sysDataObjMapper.selectBySid(sid);
    }

    @Override
    public SysDataObjExt queryBySid(String sid) throws Exception {
        return sysDataObjMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysDataObj(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysDataObj old = sysDataObjMapper.selectBySid(sid);
            super.checkEmpty(old, CommonMsgNameConstants.ERROR_MSG_RECORD_NOT_EXISTS);
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            sysDataObjMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysDataObj(SysDataObj record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.checkEmpty(record.getCode(), CommonMsgNameConstants.getEmptyMsg(CommonParamNameConstants.CODE));
            super.checkEmpty(record.getName(), CommonMsgNameConstants.getEmptyMsg(CommonParamNameConstants.NAME));
            super.autoInjectBaseData(record, opUser, TYPE_CREATE);
            sysDataObjMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, TYPE_UPDATE);
            sysDataObjMapper.updateByPrimaryKeySelective(record);
        }
    }

    @Override
    public List<SysDataObjExt> findSysDataObjs(Map<String, Object> params, PageBean page) throws Exception {
        return sysDataObjMapper.selectByPage(params, page);
    }

    @Override
    public List<SysDataObjExt> findSysDataObjs(Map<String, Object> params) throws Exception {
        return sysDataObjMapper.selectByParams(params);
    }

    @Override
    public List<SysDataObjExt> findSysDataObjAccessRuleList(Map<String, Object> params) throws Exception {
        List<SysDataObjExt> list = sysDataObjMapper.selectByParams(params);
        this.procOrderDetail(list, params);
        return list;
    }

    private void procOrderDetail(List<SysDataObjExt> list, Map<String, Object> params) {
        if (!HlpUtils.isEmptyList(list)) {
            List<String> dataObjList = ListUtil.map(list, SysDataObjExt::getSid);
            params.put(CommonParamNameConstants.DATA_OBJ_LIST, dataObjList);
            List<SysDataObjAccessRuleExt> dataObjExtList = sysDataObjAccessRuleMapper.selectEntityById(params);

            if (!HlpUtils.isEmptyList(dataObjExtList)) {
                for (SysDataObjExt orderExt : list) {
                    List<SysDataObjAccessRuleExt> filterList = ListUtil.filter(dataObjExtList, t -> orderExt.getSid().equals(t.getObj_sid()));
                    orderExt.setData_obj_access_rule_list(filterList);
                }
            }
        }
    }
}
